define(function(require, exports, module) {
	/* 加载jquery */
	var $ = require("../modules/jquery/jquery.js");
	require("../modules/plugins/moment.js");
	exports.init = function() {
		var Employeeinfo = {
			data() {

				var validatorUserName = function validatorUserName(rule, value, callback) {
					val = $.trim(value);
					var reg = '^[\u4e00-\u9fa5]{2,6}$';
					var regExp = new RegExp(reg);
					if(!regExp.test(val)) {
						return callback(new Error('姓名必须为字母或者汉字,长度为2-6'));
					} else {
						return callback();
					}
				};

				var validatorSex = function(rule, value, callback) {
					if(value === '') {
						return callback(new Error('请选择性别'));
					} else {
						return callback();
					}
				};

				var validatorPhone = function(rule, value, callback) {
					val = $.trim(value);
					var reg = '^[0-9]*$';
					var regExp = new RegExp(reg);
					if(!regExp.test(val) || val.length > "11" || val.length < "11") {
						return callback(new Error('请输入正确的11位电话号码'));
					} else {
						return callback();
					}
				};

				var validatorJobtime = function(rule, value, callback) {
					if(value === '') {
						return callback(new Error('请选择日期'));
					} else {
						return callback();
					}
				};

				var validatorDepartmentId = function(rule, value, callback) {
					if(value === '') {
						return callback(new Error('请选择一个部门'));
					} else {
						return callback();
					}
				};

				var validatorAddress = function(rule, value, callback) {
					val = $.trim(value);
					var reg = '^[\u4E00-\u9FA5A-Za-z0-9_]+$';
					var regExp = new RegExp(reg);
					if(!regExp.test(val)) {
						return callback(new Error('请输入合法的地址'));
					} else {
						return callback();
					}
				};

				return {
					cloneTableData: [],
					tableData: [],
					searchKeyword: '',
					// 日期选择器
					pickerOptions1: {
						shortcuts: [{
								text: '今天',
								onClick(picker) {
									picker.$emit('pick', new Date());
								}
							},
							{
								text: '昨天',
								onClick(picker) {

									const date = new Date();
									date.setTime(date.getTime() - 3600 * 1000 * 24);
									picker.$emit('pick', date);
								}
							}, {
								text: '一周前',
								onClick(picker) {
									const date = new Date();
									date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
									picker.$emit('pick', date);
								}
							}
						]
					},
					jobtime: '',

					/* 部门下拉 */
					department: [],
					departmentId: '医疗事业部',

					emp: {
						userId: '',
						userName: '',
						sex: '',
						phone: '',
						jobtime: '',
						departmentId: '',
						address: ''
					},
					mainStyle: {
						height: 0,
					},
					ruleForm: {
						userId: '',
						userName: '',
						sex: '',
						phone: '',
						jobtime: '',
						departmentId: '',
						address: '',
					},
					/* 表单校验 */
					rules: {
						userName: [{
								required: true,
								validator: validatorUserName,
								trigger: 'blur'
							},
							{
								min: 2,
								max: 5,
								trigger: 'blur'
							}
						],
						sex: [{
							required: true,
							validator: validatorSex,
							trigger: 'change'
						}],
						phone: [{
							required: true,
							validator: validatorPhone,
							tigger: 'blur'
						}],
						jobtime: [{
							type: 'date',
							required: true,
							validator: validatorJobtime,
							trigger: 'change'
						}],
						departmentId: [{
							required: true,
							validator: validatorDepartmentId,
							trigger: 'blur'
						}],
						address: [{
							required: true,
							validator: validatorAddress,
							trigger: 'blur'
						}]
					}
				};
			},

			/**
			 * 方法
			 */
			methods: {
				/**
				 * 选中单行事件
				 * 
				 * @param {Object}
				 *            row:选中行
				 * @param {Object}
				 *            event：
				 * @param {Object}
				 *            column
				 */
				selectRow: function(row, event, column) {
					// 此处可以归为同一对象下 没必要使用多个变量 例如 jobtime
					debugger;
					this.emp.userId = row.userId;
					this.emp.userName = row.userName;
					this.emp.sex = row.sex;
					this.emp.phone = row.phone;
					this.jobtime = row.jobtime;
					this.emp.address = row.address;
					this.departmentId = row.departmentName;
				},

				/**
				 * 表单提交 _this.$refs.ruleForm.validate:验证表单 验证完成后提交表单，成功后->重置表单
				 * 
				 * @param {Object}
				 *            ruleForm
				 */
				submitForm: function(ruleForm) {
					debugger
					var _this = this;
					_this.$refs.ruleForm.validate((valid) => {
						if(valid) {
							$.ajax({
								type: "post",
								url: "/hz-console/user/insert.do",
								async: false,
								dataType: 'json',
								data: {
									userName: this.ruleForm.userName,
									sex: this.ruleForm.sex,
									phone: this.ruleForm.phone,
									jobtime: this.ruleForm.jobtime,
									departmentId: this.ruleForm.departmentId,
									address: this.ruleForm.address,
								},
								success: function(data) {
									console.log(data);
									_this.$confirm("新建成功", '提示', {
										confirmButtonText: '确定',
										type: 'success',
										center: true,
										showClose: false,
										showCancelButton: false,
									});
									_this.$refs[ruleForm].resetFields();
									_this.selectAll();
								},
								error: function(ex) {
									alert(ex);
								}
							});
						} else {
							console.log('Erro submit!');
							// 这里return false 无意义 如果校验不成功不会进if 不会执行ajax
							return false;
						}
					});
				},

				/**
				 * 修改用户信息
				 */
				editUserInfo: function() {
					debugger
					this.openFullScreen2();
					var _this = this;
					var D_Name='' ;
					var D_Department = '';
					var D_Name = this.departmentId;
					var sex = this.emp.sex;
					var getDepartment = this.department;
					for (var i = 0;i<getDepartment.length;i++) {
						if(D_Name==getDepartment[i].departmentName){
							D_Department = getDepartment[i].departmentId;
						}
					}
					if(sex == '男') {
						sex = 1;
					}
					if(sex == '女') {
						sex = 2;
					}
					var date = new Date(_this.jobtime);
					$.ajax({
						type: "post",
						url: "/hz-console/user/updateHzUserinfo.do",
						async: false,
						dataType: 'json',
						data: {
							userId: this.emp.userId,
							userName: this.emp.userName,
							sex: sex,
							phone: this.emp.phone,
							jobtime: date,
							departmentId: D_Department,
							address: this.emp.address
						},
						success: function(data) {
							console.log("修改成功");
							this.selectAll();
						},
						error: function() {}
					});
					this.openMessageTitle();
					
				},
				/**
				 * 查询所有用户
				 */
				selectAll: function() {
					debugger
					var _this = this;
					$.ajax({
						type: "get",
						url: "/hz-console/user/getUserInfo.do",
						async: false,
						dataType: 'json',
						data: {},
						success: function(data) {
							$.each(data, function(i, obj) {
								obj.jobtime = _this.isNullOrEmpty(obj.jobtime) ? '' : moment(new Date(obj.jobtime)).format("YYYY-MM-DD")
								if(obj.sex == 1) {
									obj.sex = '男';
								} else {
									obj.sex = '女';
								}
							});
							_this.cloneTableData = data;
							_this.tableData = data;
						},
						error: function() {

						}
					});
				},
				/**
				 * 查询部门
				 */
				selectDepartment: function() {
					var _this = this;
					$.ajax({
						type: "get",
						url: "/hz-console/user/getDepartment.do",
						async: false,
						dataType: 'json',
						data: {},
						success: function(data) {
							_this.department = data;
						},
						error: function() {

						}
					});
				},

				/* 表单重置 */
				resetForm: function(ruleForm) {
					this.$nextTick(function() {
						this.$refs[ruleForm].resetFields();
					});
				},
				isNullOrEmpty: function(strVal) {
					if(strVal == '' || strVal == null || strVal == undefined) {
						return true;
					} else {
						return false;
					}
				},
				/**
				 * 用户条件检索,点击确定之后
				 */
				searchEmp: function(value) {
					var _this = this;
					var reg = '^[\u4e00-\u9fa5]$';
					var regExp = new RegExp(reg);
					var searchSession = [];
					var searchList = [];
					if(_this.searchKeyword.trim() == '') {
						searchList = _this.cloneTableData;
					}
					if(_this.searchKeyword.trim() != '') {
						searchSession = _this.cloneTableData;
					}
					$.each(searchSession, function(i, data) {
						if(data.userName.indexOf(_this.searchKeyword) != -1) {
							searchList.push(data);
						}
					});
					_this.tableData = searchList;
				},
				/**
				 * 当搜索框为空时，加载所有数据
				 */
				searchEmpNUll: function() {
					var _this = this;
					var reg = '^[\u4e00-\u9fa5]$';
					var regExp = new RegExp(reg);
					var searchList = [];
					if(_this.searchKeyword.trim() != '') {
						return false;
					}
					if(_this.searchKeyword.trim() == '') {
						_this.tableData = _this.cloneTableData;
					}
				},

				/**
				 * 加载
				 */
				openFullScreen2() {
					const loading = this.$loading({
						lock: true,
						text: 'Loading',
						spinner: 'el-icon-loading',
						background: 'rgba(0, 0, 0, 0.7)'
					});
					setTimeout(() => {
						loading.close();
					}, 2000);
				},
				/**
				 * 消息提示
				 */
				openMessageTitle() {
					this.$message({
						message: '修改成功',
						type: 'success'
					});
				},
				/**
				 * 刷新
				 */
				refreshPage(){
					window.location.reload();
				},
				
			},

			watch: {
				searchKeyword: function(newSeachKeyword) {
					this.searchEmpNUll();
				}
			},

			created: function() {
				this.mainStyle.height = $(window).height() + 1000 + 'px';
				this.selectAll();
				this.selectDepartment();
			},

		}

		var Ctor = Vue.extend(Employeeinfo);
		new Ctor().$mount('#em');
	}
})